import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { Http, Headers } from "@angular/http";
import { Fault } from "../entity/fault.model";
import { Observable } from "rxjs/Observable";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class FaultService {
    private readonly param = "faults";
    private get url(): string {
        return `${AppConfig.APP_RESTFUL}/${this.param}`;
    }
    constructor(private http: InterceptorService) { }

    getFaults(): Promise<Fault[]> {
        return this.http.get(this.url)
            .toPromise()
            .then(response => response.json().data as Fault[])
            .catch(this.handleError);
    }

    /**
     * 根据型号获得故障信息
     * 
     * @param {number} modelId 
     * @returns {Promise<Fault[]>} 
     * @memberof FaultService
     */
    getFaultsByModelId(modelId: number): Promise<Fault[]> {
        return this.http.get(`${this.url}/modelId/${modelId}`)
            .toPromise()
            .then(response => response.json().data as Fault[])
            .catch(this.handleError);
    }
    /**
     * 根据schemeId获得故障信息
     * 
     * @param {number} modelId 
     * @returns {Promise<Fault[]>} 
     * @memberof FaultService
     */
    getFaultsBySchemeId(schemeId: number): Promise<Fault[]> {
        return this.http.get(`${this.url}/schemeId/${schemeId}`)
            .toPromise()
            .then(response => response.json().data as Fault[])
            .catch(this.handleError);
    }
    create(fault: Fault): Promise<boolean> {
        return this.http.post(this.url, JSON.stringify(fault))
            .toPromise()
            .then(res => {
                try {
                    let result = res.json();
                    switch (result.code) {
                        case "200":
                            return true;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    this.handleError(error);
                }
            })
            .catch(this.handleError);
    }
    update(fault: Fault): Promise<Fault> {
        const url = `${this.url}`;
        return this.http.put(url, JSON.stringify(fault))
            .toPromise()
            .then(() => fault)
            .catch(this.handleError);
    }
    delete(id: number): Promise<void> {
        const url = `${this.url}/${id}`;
        return this.http.delete(url)
            .toPromise()
            .then(() => null)
            .catch(this.handleError);
    }
    batchDelete(valueIds: number[]): Promise<boolean> {
        const apiUrl = `${this.url}?items=${valueIds}`;
        return this.http.delete(apiUrl)
            .toPromise()
            .then(response => {
                try {
                    let result = response.json();
                    switch (result.code) {
                        case "200":
                            return true;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    throw error;
                }

            })
            .catch(this.handleError);
    }
    faultCodeService(faultCode: string,schemeId:number, faultId: number) {
        const url = `${this.url}/faultCode/${faultCode}/schemeId/${schemeId}/faultId/${faultId}`;
        return this.http.get(url).map(response => {
            let code = response.json().code;
            switch (code) {
                case "3001":
                    return true;
                case "3002":
                    return false;
            }
        }).catch(this.handleError);
    }

    faultDescService(faultDesc: string,schemeId:number, faultId: number) {
        const url = `${this.url}/faultDesc/${faultDesc}/schemeId/${schemeId}/faultId/${faultId}`;
        return this.http.get(url).map(response => {
            let code = response.json().code;
            switch (code) {
                case "3001":
                    return true;
                case "3002":
                    return false;
            }
        }).catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        return Promise.reject(error.message || error);
    }


}




// WEBPACK FOOTER //
// ./src/app/service/fault.service.ts